package com.qianfeng.wuyang.ximalaya.activity;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianfeng.wuyang.ximalaya.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ShenHao on 2016/12/4.
 */

public class Classify_Adapter extends BaseAdapter{

    private List<Entity_Classify> list_data;
    private Context context;
    private LayoutInflater inflater;

    public Classify_Adapter(List<Entity_Classify> list_data, Context context) {
        this.list_data = list_data;
        this.context = context;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return (list_data.size()+1)/2;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public View getView(int position, View contentView, ViewGroup parent) {
        MyViewHolder holder;
        if (contentView == null) {
            contentView=inflater.inflate(R.layout.layout_fragment_inlude_1,parent,false);
            holder=new MyViewHolder(contentView);
            contentView.setTag(holder);
        }else{
            holder= (MyViewHolder) contentView.getTag();
        }
        if (position==0) {
            LinearLayout parent1= (LinearLayout) holder.image_fragment_2.getParent();
            parent1.setVisibility(View.GONE);

            LinearLayout parent2= (LinearLayout) holder.image_frament.getParent();
            parent2.setVisibility(View.GONE);
        }else{
            Picasso.with(context).load(list_data.get(2*position-1).getCoverPath()).into(holder.image_frament);
            holder.textView.setText(list_data.get(2*position-1).getTitle());

            Picasso.with(context).load(list_data.get(2*position).getCoverPath()).into(holder.image_fragment_2);
            holder.textView_2.setText(list_data.get(2*position).getTitle());

//        //添加空行
                LinearLayout parent_1 = (LinearLayout) holder.image_frament.getParent();
                LinearLayout parent_2 = (LinearLayout) holder.image_fragment_2.getParent();

                LinearLayout.LayoutParams params_1 = (LinearLayout.LayoutParams) parent_1.getLayoutParams();
                LinearLayout.LayoutParams params_2 = (LinearLayout.LayoutParams) parent_2.getLayoutParams();
            if (position%3==0) {
                params_1.setMargins(0,0,1,30);
                params_2.setMargins(1,0,0,30);
            }else{
                params_1.setMargins(0,0,1,2);
                params_2.setMargins(1,0,0,1);
            }
                parent_1.setLayoutParams(params_1);
                parent_2.setLayoutParams(params_2);
        }
        //////////////////////////////////////////////////////////////////////////
        return contentView;
    }

    public class MyViewHolder{
        ImageView image_frament, image_fragment_2;
        TextView textView, textView_2;
        public MyViewHolder(View itemView) {

            image_frament = (ImageView) itemView.findViewById(R.id.include_classify_image);
            textView = (TextView) itemView.findViewById(R.id.include_classify_text);

            image_fragment_2 = (ImageView) itemView.findViewById(R.id.include_classify_image_2);
            textView_2 = (TextView) itemView.findViewById(R.id.include_classify_text_2);
        }
    }
}
